package com.company.clientscard.main.widgets.adminForm;

import com.company.clientscard.main.widgets.general.SelectItemDialogBox;
import com.company.clientscard.main.widgets.general.TableDataProvider;
import com.company.clientscard.main.widgets.general.TableManager;
import com.company.clientscard.shared.proxy.ClientProxy;
import com.company.clientscard.shared.proxy.UserProxy;
import com.company.clientscard.shared.proxyManager.ClientProxyManager;
import com.company.clientscard.shared.proxyManager.ProxyManager;
import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UsersTable extends VerticalPanel {
	private TableManager<UserProxy> tableManager;

	private CellTable<UserProxy> table = new CellTable<UserProxy>();
	private TextColumn<UserProxy> nameColumn = new TextColumn<UserProxy>() {
		@Override
		public String getValue(UserProxy user) {
			return user.getLogin();
		}
	};

	private TextColumn<UserProxy> passwordColumn = new TextColumn<UserProxy>() {

		@Override
		public String getValue(UserProxy user) {
			// TODO Auto-generated method stub
			return user.getPassword();
		}
	};

	private TextColumn<UserProxy> roleColumn = new TextColumn<UserProxy>() {
		@Override
		public String getValue(UserProxy user) {
			// TODO Auto-generated method stub
			return user.getRole();
		}
	};

	private TextColumn<UserProxy> clientColumn = new TextColumn<UserProxy>() {
		@Override
		public String getValue(UserProxy user) {
			// TODO Auto-generated method stub
			if (user.getClient() == null) {
				return "NULL";
			} else {
				return user.getClient().getName();
			}
		}
	};

	ActionCell<UserProxy> helloCell = new ActionCell<UserProxy>("Edit client",
			new ActionCell.Delegate<UserProxy>() {
				@Override
				public void execute(final UserProxy object) {

					final SelectItemDialogBox<ClientProxy> si = new SelectItemDialogBox<ClientProxy>(
							new TableDataProvider<ClientProxy>(
									new ClientProxyManager()),
							object.getClient()) {

						@Override
						public String getName(ClientProxy object) {
							// TODO Auto-generated method stub
							return object.getName();
						}
					};
					si.addCloseHandler(new CloseHandler<PopupPanel>() {

						@Override
						public void onClose(CloseEvent<PopupPanel> event) {
							// TODO Auto-generated method stub
							if (si.isSelected()) {
								ClientProxy client = si.getSelectedItem();
								ProxyManager<UserProxy> manager = tableManager
										.getManager();
								UserProxy editUser = manager
										.getProxyObject(object);
								editUser.setClient(client);
								manager.update(editUser, table);
							}
						}
					});

					si.center();
					si.show();

				}
			});

	Column<UserProxy, UserProxy> helloColumn = new Column<UserProxy, UserProxy>(
			helloCell) {

		@Override
		public UserProxy getValue(UserProxy object) {
			// TODO Auto-generated method stub
			return object;
		}
	};

	private void initTable() {
		table.setWidth("100%");

		table.addColumn(nameColumn, "Name");
		table.addColumn(passwordColumn, "Password");
		table.addColumn(roleColumn, "Role");
		table.addColumn(clientColumn, "Client");

		table.addColumn(helloColumn, "");

		table.setPageSize(5);

		SimplePager pager = new SimplePager();
		pager.setPageSize(5);
		pager.setDisplay(table);

		setWidth("100%");
		setHorizontalAlignment(ALIGN_CENTER);
		add(table);
		add(pager);

		tableManager.addDataDisplay(table);
	}

	public UsersTable(TableManager<UserProxy> tableManager) {
		this.tableManager = tableManager;
		initTable();

		// tableManager.addDataDisplay(table);
	}

	public TableManager<UserProxy> getTableManager() {
		return tableManager;
	}
}
